﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExpressionEngine
{
    /// <summary>
    /// 基础数据函数处理器
    /// </summary>
    public class BaseMath : ExpressionHandler
    {
        /// <summary>
        ///  计算三角函数的正切值
        /// </summary>
        public  Variable TAN(Variable var)
        {
            if (!base.bNumberical(var))
                throw new Exception("三角函数参数类型错误");

            return base.ForName(new Variable(){Value = Math.Tan(double.Parse(var.Value.ToString()) * Math.PI / 180), ValueType = DataType.DOUBLE}); 
        }

        /// <summary>
        /// 条件判断函数
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="trueResult"></param>
        /// <param name="falseResult"></param>
        /// <returns></returns>
        public Variable IF_ELSE(Variable falseResult, Variable trueResult, Variable condition)
        {
            if (condition.ValueType != DataType.BOOL || condition.Value.GetType() != typeof(bool))
                throw new Exception("判断语句的条件计算结果类型错误");

            if (condition.Value.Equals(true))
                return base.ForName(trueResult);
            else
                return base.ForName(falseResult);
        }

        /// <summary>
        /// OR函数
        /// </summary>
        /// <param name="condition1"></param>
        /// <param name="condition2"></param>
        /// <returns></returns>
        public Variable OR(Variable condition1, Variable condition2)
        {
            if (condition1.ValueType != DataType.BOOL || condition2.ValueType != DataType.BOOL)
                throw new Exception("判断语句的条件计算结果类型错误");

            if (condition1.Value.Equals(true) || condition2.Value.Equals(true))
                return base.ForName(new Variable() { Value = true, ValueType = DataType.BOOL });
            else
                return base.ForName(new Variable() { Value = false, ValueType = DataType.BOOL});
        }

        /// <summary>
        /// AND函数
        /// </summary>
        /// <param name="condition1"></param>
        /// <param name="condition2"></param>
        /// <returns></returns>
        public Variable AND(Variable condition1, Variable condition2)
        {
            if (condition1.ValueType != DataType.BOOL || condition2.ValueType != DataType.BOOL)
                throw new Exception("判断语句的条件计算结果类型错误");

            if (condition1.Value.Equals(true) && condition2.Value.Equals(true))
                return base.ForName(new Variable() { Value = true, ValueType = DataType.BOOL });
            else
                return base.ForName(new Variable() { Value = false, ValueType = DataType.BOOL });
        }

        /// <summary>
        /// NOT函数
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Variable NOT(Variable condition)
        {
            if (condition.ValueType != DataType.BOOL)
                throw new Exception("判断语句的条件计算结果类型错误");

            if (condition.Value.Equals(true))
                return base.ForName(new Variable() { Value = false, ValueType = DataType.BOOL });
            else
                return base.ForName(new Variable() { Value = true, ValueType = DataType.BOOL });
        }
    }
}
